package com.zxt.dlna.activity.device;


import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.zxt.dlna.dmp.DeviceItem;

import org.fourthline.cling.model.meta.LocalDevice;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.registry.DefaultRegistryListener;
import org.fourthline.cling.registry.Registry;

import java.util.ArrayList;

public class DeviceListRegistryListener extends DefaultRegistryListener {

    private final DevAdapter mDmrDevAdapter;
    private final ArrayList<DeviceItem> mDmrList;
    private final DevAdapter mDevAdapter;

    private final ArrayList<DeviceItem> mDevList;
    private final String TAG = "RegistryListener";

    public DeviceListRegistryListener(ArrayList<DeviceItem> mDmrList, DevAdapter mDmrDevAdapter,
                                      ArrayList<DeviceItem> mDevList, DevAdapter mDevAdapter) {
        this.mDmrDevAdapter = mDmrDevAdapter;
        this.mDmrList = mDmrList;
        this.mDevAdapter = mDevAdapter;
        this.mDevList = mDevList;
    }

    @Override
    public void remoteDeviceDiscoveryStarted(Registry registry,
                                             RemoteDevice device) {
        Log.d(TAG, "remoteDeviceDiscoveryStarted: " + device.toString());
    }

    @Override
    public void remoteDeviceDiscoveryFailed(Registry registry,
                                            final RemoteDevice device, final Exception ex) {
        Log.d(TAG, "remoteDeviceDiscoveryFailed: ", ex);
    }

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private void runOnUiThread(Runnable runnable) {
        mHandler.post(runnable);
    }

    /*
     * End of optimization, you can remove the whole block if your Android
     * handset is fast (>= 600 Mhz)
     */

    @Override
    public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
        Log.e(TAG, "remoteDeviceAdded:" + device.toString() + device.getType().getType());

        if (device.getType().getNamespace().equals("schemas-upnp-org")
                && device.getType().getType().equals("MediaServer")) {
            final DeviceItem display = new DeviceItem(device, device
                    .getDetails().getFriendlyName(),
                    device.getDisplayString(), "(REMOTE) "
                    + device.getType().getDisplayString());
            deviceAdded(display);
        }

        if (device.getType().getNamespace().equals("schemas-upnp-org")
                && device.getType().getType().equals("MediaRenderer")) {
            final DeviceItem dmrDisplay = new DeviceItem(device, device
                    .getDetails().getFriendlyName(),
                    device.getDisplayString(), "(REMOTE) "
                    + device.getType().getDisplayString());
            dmrAdded(dmrDisplay);
        }
    }

    @Override
    public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
        final DeviceItem display = new DeviceItem(device,
                device.getDisplayString());
        deviceRemoved(display);

        if (device.getType().getNamespace().equals("schemas-upnp-org")
                && device.getType().getType().equals("MediaRenderer")) {
            final DeviceItem dmrDisplay = new DeviceItem(device, device
                    .getDetails().getFriendlyName(),
                    device.getDisplayString(), "(REMOTE) "
                    + device.getType().getDisplayString());
            dmrRemoved(dmrDisplay);
        }
    }

    @Override
    public void localDeviceAdded(Registry registry, LocalDevice device) {
        Log.e(TAG, "localDeviceAdded:" + device.toString() + device.getType().getType());

        final DeviceItem display = new DeviceItem(device, device
                .getDetails().getFriendlyName(), device.getDisplayString(),
                "(REMOTE) " + device.getType().getDisplayString());
        deviceAdded(display);
    }

    @Override
    public void localDeviceRemoved(Registry registry, LocalDevice device) {
        Log.e("RegistryListener",
                "localDeviceRemoved:" + device.toString()
                        + device.getType().getType());

        final DeviceItem display = new DeviceItem(device, device.getDisplayString());
        deviceRemoved(display);
    }

    public void deviceAdded(final DeviceItem di) {
        if (mDevList != null && !mDevList.contains(di)) {
            mDevList.add(di);
            if (mDevAdapter != null) {
                runOnUiThread(() -> mDevAdapter.notifyDataSetChanged());
            }
        }
    }

    public void deviceRemoved(final DeviceItem di) {
        if (mDevList != null) {
            mDevList.remove(di);
            if (mDevAdapter != null) {
                runOnUiThread(() -> mDevAdapter.notifyDataSetChanged());
            }
        }
    }

    public void dmrAdded(final DeviceItem di) {
        if (mDmrList != null && !mDmrList.contains(di)) {
            mDmrList.add(di);
            if (mDmrDevAdapter != null) {
                runOnUiThread(() -> mDmrDevAdapter.notifyDataSetChanged());
            }
        }
    }

    public void dmrRemoved(final DeviceItem di) {
        if (mDmrList != null) {
            mDmrList.remove(di);
        }
        if (mDmrDevAdapter != null) {
            runOnUiThread(() -> mDmrDevAdapter.notifyDataSetChanged());
        }
    }
}

